package cn.itcalvin.sncoal.cth.utils;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;

/**
 * @Classname SnowFlakeUtils
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/10/18 21:32
 * @Created by CALVIN
 */
public class SnowFlakeUtils {
    private long workerId = 1;//为终端ID
    private long datacenterId = 1;//数据中心ID
    private Snowflake snowflake;
    /**
     * 构造
     */
    SnowFlakeUtils(){
//        workerId = NetUtil.ipv4ToLong(NetUtil.getLocalhostStr());
        snowflake= IdUtil.getSnowflake(workerId,datacenterId);
    }

    public Long snowflakeId(){
        return snowflake.nextId();
    }

    private static  SnowFlakeUtils snowFlakeUtils;


    /**
     * 获取生成的ID
     * @return
     */
    public static Long getId(){
        if(snowFlakeUtils==null){
            snowFlakeUtils=new SnowFlakeUtils();
        }
        try{
            return snowFlakeUtils.snowflakeId();
        }
        catch (Exception e){
            snowFlakeUtils=new SnowFlakeUtils();
            return snowFlakeUtils.snowflakeId();
        }
    }
}
